import 'package:flutter/material.dart';
import '../../config/theme/theme_manager.dart';

/// 通用卡片组件
/// 提供统一的卡片样式和布局
class CommonCard extends StatelessWidget {
  /// 子组件
  final Widget child;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 外边距
  final EdgeInsetsGeometry? margin;

  /// 宽度
  final double? width;

  /// 高度
  final double? height;

  /// 背景颜色
  final Color? backgroundColor;

  /// 边框半径
  final double? borderRadius;

  /// 边框
  final Border? border;

  /// 阴影
  final List<BoxShadow>? boxShadow;

  /// 渐变色
  final Gradient? gradient;

  /// 点击回调
  final VoidCallback? onTap;

  /// 长按回调
  final VoidCallback? onLongPress;

  /// 是否显示阴影
  final bool showShadow;

  /// 阴影强度
  final ShadowIntensity shadowIntensity;

  /// 是否显示边框
  final bool showBorder;

  /// 卡片类型
  final CardType cardType;

  const CommonCard({
    super.key,
    required this.child,
    this.padding,
    this.margin,
    this.width,
    this.height,
    this.backgroundColor,
    this.borderRadius,
    this.border,
    this.boxShadow,
    this.gradient,
    this.onTap,
    this.onLongPress,
    this.showShadow = true,
    this.shadowIntensity = ShadowIntensity.medium,
    this.showBorder = false,
    this.cardType = CardType.normal,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    // 计算背景颜色
    final finalBackgroundColor = _getBackgroundColor(themeManager);

    // 计算阴影
    final finalBoxShadow = _getBoxShadow(themeManager);

    // 计算边框
    final finalBorder = _getBorder(themeManager);

    // 计算边框半径
    final finalBorderRadius = _getBorderRadius();

    Widget cardWidget = Container(
      width: width,
      height: height,
      margin: margin,
      decoration: BoxDecoration(
        color: gradient == null ? finalBackgroundColor : null,
        gradient: gradient,
        borderRadius: BorderRadius.circular(finalBorderRadius),
        border: finalBorder,
        boxShadow: finalBoxShadow,
      ),
      child: ClipRRect(
        borderRadius: BorderRadius.circular(finalBorderRadius),
        child: Container(
          padding: padding ?? _getDefaultPadding(),
          child: child,
        ),
      ),
    );

    // 如果有点击事件，包装在InkWell中
    if (onTap != null || onLongPress != null) {
      cardWidget = Material(
        color: Colors.transparent,
        child: InkWell(
          onTap: onTap,
          onLongPress: onLongPress,
          borderRadius: BorderRadius.circular(finalBorderRadius),
          child: cardWidget,
        ),
      );
    }

    return cardWidget;
  }

  /// 获取背景颜色
  Color _getBackgroundColor(ThemeManager themeManager) {
    if (backgroundColor != null) {
      return backgroundColor!;
    }

    switch (cardType) {
      case CardType.normal:
        return themeManager.cardColor;
      case CardType.primary:
        return themeManager.primaryColor.withOpacity(0.1);
      case CardType.success:
        return Colors.green.withOpacity(0.1);
      case CardType.warning:
        return Colors.orange.withOpacity(0.1);
      case CardType.danger:
        return Colors.red.withOpacity(0.1);
      case CardType.info:
        return Colors.blue.withOpacity(0.1);
    }
  }

  /// 获取阴影
  List<BoxShadow>? _getBoxShadow(ThemeManager themeManager) {
    if (boxShadow != null) {
      return boxShadow;
    }

    if (!showShadow) {
      return null;
    }

    final shadowColor = themeManager.isDarkMode
        ? Colors.black.withOpacity(0.3)
        : Colors.grey.withOpacity(0.2);

    switch (shadowIntensity) {
      case ShadowIntensity.light:
        return [
          BoxShadow(
            color: shadowColor,
            blurRadius: 4,
            offset: const Offset(0, 1),
          ),
        ];
      case ShadowIntensity.medium:
        return [
          BoxShadow(
            color: shadowColor,
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ];
      case ShadowIntensity.strong:
        return [
          BoxShadow(
            color: shadowColor,
            blurRadius: 16,
            offset: const Offset(0, 4),
          ),
        ];
    }
  }

  /// 获取边框
  Border? _getBorder(ThemeManager themeManager) {
    if (border != null) {
      return border;
    }

    if (!showBorder) {
      return null;
    }

    return Border.all(
      color: themeManager.dividerColor,
      width: 1,
    );
  }

  /// 获取边框半径
  double _getBorderRadius() {
    return borderRadius ?? 12.0;
  }

  /// 获取默认内边距
  EdgeInsetsGeometry _getDefaultPadding() {
    switch (cardType) {
      case CardType.normal:
        return const EdgeInsets.all(16);
      case CardType.primary:
      case CardType.success:
      case CardType.warning:
      case CardType.danger:
      case CardType.info:
        return const EdgeInsets.all(12);
    }
  }
}

/// 卡片类型
enum CardType {
  normal,
  primary,
  success,
  warning,
  danger,
  info,
}

/// 阴影强度
enum ShadowIntensity {
  light,
  medium,
  strong,
}

/// 信息卡片组件
class InfoCard extends StatelessWidget {
  final String title;
  final String? subtitle;
  final String value;
  final IconData? icon;
  final Color? iconColor;
  final VoidCallback? onTap;
  final CardType cardType;

  const InfoCard({
    super.key,
    required this.title,
    this.subtitle,
    required this.value,
    this.icon,
    this.iconColor,
    this.onTap,
    this.cardType = CardType.normal,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    return CommonCard(
      cardType: cardType,
      onTap: onTap,
      child: Row(
        children: [
          if (icon != null) ...[
            Container(
              padding: const EdgeInsets.all(8),
              decoration: BoxDecoration(
                color:
                    (iconColor ?? themeManager.primaryColor).withOpacity(0.1),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                icon,
                color: iconColor ?? themeManager.primaryColor,
                size: 24,
              ),
            ),
            const SizedBox(width: 12),
          ],
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 14,
                    color: themeManager.contrastColor.withOpacity(0.7),
                    fontWeight: FontWeight.w500,
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  value,
                  style: TextStyle(
                    fontSize: 20,
                    color: themeManager.contrastColor,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                if (subtitle != null) ...[
                  const SizedBox(height: 2),
                  Text(
                    subtitle!,
                    style: TextStyle(
                      fontSize: 12,
                      color: themeManager.contrastColor.withOpacity(0.5),
                    ),
                  ),
                ],
              ],
            ),
          ),
          if (onTap != null)
            Icon(
              Icons.arrow_forward_ios,
              size: 16,
              color: themeManager.contrastColor.withOpacity(0.3),
            ),
        ],
      ),
    );
  }
}

/// 统计卡片组件
class StatCard extends StatelessWidget {
  final String title;
  final String value;
  final String? unit;
  final IconData icon;
  final Color? color;
  final String? trend;
  final bool isPositiveTrend;
  final VoidCallback? onTap;

  const StatCard({
    super.key,
    required this.title,
    required this.value,
    this.unit,
    required this.icon,
    this.color,
    this.trend,
    this.isPositiveTrend = true,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;
    final cardColor = color ?? themeManager.primaryColor;

    return CommonCard(
      onTap: onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                title,
                style: TextStyle(
                  fontSize: 14,
                  color: themeManager.contrastColor.withOpacity(0.7),
                  fontWeight: FontWeight.w500,
                ),
              ),
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: cardColor.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Icon(
                  icon,
                  color: cardColor,
                  size: 20,
                ),
              ),
            ],
          ),
          const SizedBox(height: 12),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text(
                value,
                style: TextStyle(
                  fontSize: 24,
                  color: themeManager.contrastColor,
                  fontWeight: FontWeight.bold,
                ),
              ),
              if (unit != null) ...[
                const SizedBox(width: 4),
                Text(
                  unit!,
                  style: TextStyle(
                    fontSize: 14,
                    color: themeManager.contrastColor.withOpacity(0.7),
                  ),
                ),
              ],
            ],
          ),
          if (trend != null) ...[
            const SizedBox(height: 8),
            Row(
              children: [
                Icon(
                  isPositiveTrend ? Icons.trending_up : Icons.trending_down,
                  size: 16,
                  color: isPositiveTrend ? Colors.green : Colors.red,
                ),
                const SizedBox(width: 4),
                Text(
                  trend!,
                  style: TextStyle(
                    fontSize: 12,
                    color: isPositiveTrend ? Colors.green : Colors.red,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ],
            ),
          ],
        ],
      ),
    );
  }
}
